package com.cpe.web.intercepter;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.cpe.web.intercepter.factory.GlobalInterceptorFactory;

/**
 * 全局拦截器
 * 
 * @description TODO
 * @author zhoud
 * @date 2015年11月10日 下午1:46:38
 */
public class PreInterceptor extends HandlerInterceptorAdapter {

	private GlobalInterceptorFactory factory;

	public PreInterceptor() {
		factory = GlobalInterceptorFactory.instance();
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		List<HandlerInterceptor> interceptors = factory.getPreInterceptors();
		for (HandlerInterceptor interceptor : interceptors) {
			// 只要有一个没通过，就中断请求
			if (!interceptor.preHandle(request, response, handler)) {
				return false;
			}
		}
		return true;
	}

}
